/**
 * 
 */
package org.gov.vis.gui.util;

import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.util.List;

import javax.swing.JLabel;
import javax.swing.JPanel;

/**
 * @author Muralidhar Yaragalla
 *
 */
public class ErrorsDisplayJPanel extends JPanel{

	private static final long serialVersionUID = 8698965649281059194L;	
	private JPanel parentPanel;
	
	/**
	 * This sets Grid Layout to the panel.
	 * @param exceptions
	 */
	public ErrorsDisplayJPanel(int rows){
		setLayout(new GridBagLayout());
	}
	

	public ErrorsDisplayJPanel(JPanel getjPanel1) {
		// TODO Auto-generated constructor stub
		GridBagLayout jPanel1Layout = new GridBagLayout();
		jPanel1Layout.rowWeights = new double[] {0.1};
		jPanel1Layout.rowHeights = new int[] {7};
		jPanel1Layout.columnWeights = new double[] {0.1};
		jPanel1Layout.columnWidths = new int[] {7};
		this.setLayout(jPanel1Layout);
		
		this.parentPanel = getjPanel1;
		
		
		getjPanel1.setVisible(true);
		getjPanel1.add(this,new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));
	}


	/**
	 * This adds a error message label to the panel.
	 * @param errMsg
	 */
	public void addErrMsg(String errMsg){
		JLabel msgLabel=new JLabel(errMsg);
		msgLabel.setForeground(Color.RED);
		add(msgLabel);
	}

	public void setErrorMessages(List<String> errorMsgLst) {
		displayMsgsOnPanel(errorMsgLst,Color.RED);
	}

	private void displayMsgsOnPanel(List<String> msgLst, Color color) {
		if(msgLst==null || msgLst.isEmpty()){
			return;
		}
		
		
		parentPanel.removeAll();
		parentPanel.revalidate();
		parentPanel.repaint();
		
		for (int i = 0; i < msgLst.size(); i++) {
			System.out.println(msgLst.get(i));
			JLabel msgLabel=new JLabel(msgLst.get(i));
			msgLabel.setForeground(color);
			this.add(msgLabel, new GridBagConstraints(0, i, 1, 1, 0.0, 0.0, GridBagConstraints.EAST, GridBagConstraints.BOTH, new Insets(0, 0, 5, 0), 0, 0));
			this.setVisible(true);
		}
		parentPanel.add(this,new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));
		parentPanel.setVisible(true);
	}
	
	
	public void setSuccessMessages(List<String> errorMsgLst){
		displayMsgsOnPanel(errorMsgLst, Color.GREEN);
	}

	public void clearMsgPanel() {
		parentPanel.removeAll();
		parentPanel.revalidate();
		parentPanel.repaint();
	}
	
}
